<?php

include("gen.php");
$cmd = get_datan("cmd");
switch ($cmd) {
    case 1:
        get_health();
        break;

    case 2:
        update();
        break;

    case 3:
        answers();
        break;

    case 4:
        delete();
        break;

    case 5:
        addAnswer();
        break;
    
    default:
        echo "{";
        echo jsonn("result", 0) . ",";
        echo jsons("message", "unknown command");
        echo "}";
}

function get_health() {
    include_once("healthclass.php");
    include_once("subdistrict.php");
    $id = get_datan("id");

    $h = new healthclass();
    $subd = new subdistrict();
    $h->get_health_by_id($id);
    $row = $h->fetch();
    $subd->get_subdistrict_by_id($row['subdistrict_id']);
    $subdrow = $subd->fetch();
    if (!$row) {
        echo "{";
        echo jsonn("result", 0) . ",";
        echo jsons("message", "vaccine not found");
        echo "}";
        return;
    }

    echo "{";
    echo jsonn("result", 1) . ",";
    echo '"health":{';
    echo jsonn("id", $row['idhealth_promotion']) . ",";
    echo jsons("date", $row['date']) . ",";
    echo jsons("venue", $row['venue']) . ",";
    echo jsons("topic", $row['topic']) . ",";
    echo jsons("method", $row['method']) . ",";
    echo jsons("targetAudience", $row['target_audience']) . ",";
    echo jsonn("numAudience", $row['number_of_audience']) . ",";
    echo jsons("remarks", $row['remarks']) . ",";
    echo jsons("month", $row['month']) . ",";
    echo jsonn("latitude", $row['latitude']) . ",";
    echo jsonn("longitude", $row['longitude']) . ",";
    echo jsons("image", $row['image']) . ",";
    echo jsonn("subdistrict", $row['subdistrict_id']);
    echo "}";
    echo "}";
}

function update() {
    include_once("healthclass.php");
    $v = new healthclass();
    $id = get_datan("id");
    $topic = get_data("topic");
    $district = get_datan("district");
    $audience = get_data("audience");
    $venue = get_data("venue");
    $longitude = get_datan("long");
    $latitude = get_datan("lat");
    $method = get_data("method");
    $numApp = get_datan("numApp");
    $remarks = get_data("remarks");
    $file = get_data("file");
    $date = get_data("date");
    $month = get_data("month");

    $v->update_health($id, $date, $venue, $topic, $method, $audience, $numApp, $remarks, $month, $latitude, $longitude, $file, $district);

}

function answers() {
    include_once("questionanswerclss.php");
    $v = new questionanswerclss();
    $id = get_datan("id");
    $v->get_answer_by_id($id);
    $row = $v->fetch();
    if (!$row) {
        echo "{";
        echo jsonn("result", 0) . ",";
        echo jsons("message", "answer not found");
        echo "}";
        return;
    }

    echo "{";

    echo jsonn("result", 1) . ",";

    echo '"answer":[';
    while ($row) {
        echo "{";


        echo jsons("name", $row['name']) . "}";
        $row = $v->fetch();
        if ($row) {
            echo ",";
        }     
    }

    echo "]";
    echo "}";
}

function delete() {
    include_once ("healthclass.php");
    $q = new healthclass();
    $id = get_datan("id");
    $q->delete($id);
}

function addAnswer()
{
    include_once ("questionanswerclss.php");
    include_once ("answerclass.php");
    $ans = get_data("ans");
    $qid = get_datan("qid");
    $qa = new questionanswerclss();
    $answer = new answerclass();
    
    if(!$answer->add_answer(1,$ans))
    {
        print mysql_error();
    }
    $aid = $answer->get_insert_id($answer);
    print($qid);
    if(!$qa->add_question_and_answer($qid, $aid))
    {
         print mysql_error();
    }
    
    
}
?>